<script setup>
import loginCheck from '@/utils/loginCheck.utils';
import { onMounted, ref } from 'vue'
import { computed, onBeforeMount } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { delCookie, getCookie } from '@/utils/cookie.utils';

const router = useRouter()
const store = useStore()
let avatar = null

let nickname = null

function switchTo(routerName) {
    console.log(avatar, nickname)
    console.log(routerName)
    router.push(routerName)
}

function exit() {
    console.log("exit")
    delCookie("token")
    delCookie('user')
    router.push('login')
}

onBeforeMount(() => {
    let user = getCookie('user')
    console.log("跳转", user)
    // 我存了个null进去？
    if (user == null || user == 'null') {
        console.log("login")
        router.push("/login")
        return
    }
    user = JSON.parse(user)
    // console.log("commitUser")
    store.commit('updateUser', user)

    let model = getCookie('model')
    let temperature = getCookie('temperature')

    if (model != null) {
        store.commit("updateModel", model)
    }

    if (temperature != null) {
        store.commit("updateTemperature", parseFloat(temperature))
    }

    nickname = computed(() => {
        return store.getters.getNickname;
    })
    avatar = computed(() => {
        return import.meta.env.VITE_STATIC_ADDR + "avatar/" + store.getters.getAvatar;
    })
})

</script>

<template>
    <div>
        <div id="user" @click="switchTo('user')">
            <div id="avatar">
                <el-avatar :src="avatar" style="width:100%;height:100%;" />
            </div>
            <div id="nickname">
                {{ nickname }}
            </div>
            <div id="line" />
        </div>
        <div id="history">

        </div>
        <div id="menu">
            <div @click="switchTo('index')" class="menu-item">
                <div class="icon">
                    <el-icon>
                        <House />
                    </el-icon>
                </div>
                <txt>开启问答</txt>
            </div>
            <div @click="switchTo('user')" class="menu-item">
                <div class="icon">
                    <el-icon>
                        <User />
                    </el-icon>
                </div>
                <txt>账号管理</txt>
            </div>
            <div @click="switchTo('group')" class="menu-item">
                <div class="icon">
                    <el-icon>
                        <More />
                    </el-icon>
                </div>
                <txt>群组管理</txt>
            </div>
            <div @click="switchTo('knowledge')" class="menu-item">
                <div class="icon">
                    <el-icon>
                        <Document />
                    </el-icon>
                </div>
                <txt>知识管理</txt>
            </div>
            <div @click="switchTo('setting')" class="menu-item">
                <div class="icon">
                    <el-icon>
                        <Setting />
                    </el-icon>
                </div>
                <txt>问答设置</txt>
            </div>
            <div @click="exit()" class="menu-item">
                <div class="icon">
                    <el-icon>
                        <SwitchButton />
                    </el-icon>
                </div>
                <txt>退出登陆</txt>
            </div>
        </div>
        <div id="share-item">
            <div class="share-icon">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-github"
                    viewBox="0 0 16 16">
                    <path
                        d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
                </svg>
            </div>
            <a href="https://gitee.com/esheeper/CB-GPT.git" target="_blank">项目地址</a>
        </div>
    </div>
</template>

<style scoped>
#user {
    height: 15%;
    width: 80%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

#history {
    height: 35%;
}

#menu {
    height: 40%;
    width: 100%;
    font-size: 1.0em;
    font-weight: 600;
}

.menu-item {
    margin-left: 2px;
    margin-right: 2px;
    width: 100%;
    height: 15%;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    padding-top: 0.2em;
    padding-bottom: 0.2em;
    /* background-color: #CCFFFF; */
}

.menu-item:hover {
    cursor: pointer;
    background-color: #CCCCFF;
    font-size: 1.1em;
    font-weight: 700;
}

#share-item {
    width: 100%;
    margin-top: 2ex;
    font-size: 0.8em;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
}

.share-icon {
    margin-left: 1em;
}


a {
    margin-left: -40%;
    display: inline;
    color: rgb(211, 219, 219);
    font-weight: 500;
    padding-bottom: 8px;
}

a:hover {
    text-decoration: none;
    color: #000;
    font-weight: 800;
}

#avatar {
    width: 100px;
    height: 100px;
    border-radius: 50px;
    overflow: hidden;
}

#avatar:hover {
    width: 110px;
    height: 110px;
    border-radius: 55px;
    cursor: pointer;
}

#nickname {
    margin-top: 10px;
    font-weight: 700;
    font-size: larger;
}

.icon {
    width: fit-content;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

txt {
    align-self: center;
}

#line {
    margin-top: 5px;
    width: 100%;
    height: 0;
    border-top: 1px solid var(--el-border-color);
}

#line2 {
    margin-top: 5px;
    width: 80%;
    margin: auto;
    height: 0;
    border-top: 1px solid var(--el-border-color);
}
</style>
